import { IDictModel } from '@/api/sys/model/dictModel';

const cdnurl = '';
/**
 *  获取文件服务访问路径
 * @param fileUrl 文件路径
 * @param prefix(默认http)  文件路径前缀 http/https
 */
export const getFileAccessHttpUrl = (fileUrl, prefix = 'http') => {
  let result = fileUrl;

  if (fileUrl && fileUrl.length > 0 && !fileUrl.startsWith(prefix)) {
    //判断是否是数组格式
    const isArray = fileUrl.indexOf('[') != -1;
    if (!isArray) {
      const prefix = cdnurl;
      if (!fileUrl.startsWith('http')) {
        // 判断是否已包含前缀
        if (!fileUrl.startsWith(prefix)) {
          result = `${prefix}${fileUrl}`;
          // const res = await defHttp.postForm({ url: '/sys/file/info', params: { relativePaths: fileUrl, includeFileName: true } });
          // if (res.length > 0) {
          //   result = res[0]['url'];
          // }
        }
      }
    }
  }

  return result;
};

export const userErrorType = (msg: string, type = 'userError') => {
  return { msg, type };
};
export const converToUrl = (requestParams: Indexable<string>) => {
  const params: Array<string> = [];
  Object.entries(requestParams).forEach(([key, value]) => {
    const param: string = key + '=' + value;
    params.push(param);
  });
  return '?' + params.join('&');
};

/**
 * 字段数组转map
 */
export function dictArray2Map(dictArray: IDictModel[]) {
  return new Map(dictArray.map((dict) => [dict.value, dict.label]));
}
